package com.bang.controller;

import com.bang.entity.Goods;
import com.bang.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * (Goods)表控制层
 *
 * @author makejava
 * @since 2020-05-16 13:30:37
 */
@Controller
public class GoodsController {
    /**
     * 服务对象
     */
    @Autowired
    private GoodsService goodsService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @RequestMapping("selectOne")
    public Goods selectOne(Integer id) {
        return this.goodsService.queryById(id);
    }


    @RequestMapping("/goods")
    public String list(Model model){
        List<Goods> goods=goodsService.queryAll();
        model.addAttribute("goods",goods);
        return "emp/goods";
    }

    @GetMapping("/add")
    public String toAdd(){
        return "emp/add";
    }

    @PostMapping("/add")
    public String AddEmployee(Goods goods){
        goodsService.insert(goods);
        return "redirect:/goods";
    }

    @GetMapping("/update/{gid}")
    public String toUpdate(@PathVariable("gid")Integer gid,Model model){

        Goods goods = goodsService.queryById(gid);
        model.addAttribute("goods",goods);

        return "emp/update";
    }

    @PostMapping("/update")
    public String updateEmp(Goods goods){
        goodsService.update(goods);
        return "redirect:/goods";
    }

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id")int id){
        goodsService.deleteById(id);
        return "redirect:/goods";
    }

}